package miku.firstgame.model.proxy
{
	import miku.firstgame.constrants.AppConstrants;
	import miku.firstgame.constrants.ProtocalConstants;
	import miku.firstgame.model.User;
	import miku.firstgame.model.vo.TalkVo;
	import miku.firstgame.net.IConnection;
	import miku.firstgame.net.pack.TalkResponsePack;
	import miku.firstgame.net.proxy.Connection;

	import org.puremvc.as3.patterns.proxy.Proxy;

	public class TalkProxy extends Proxy
	{
		public static const NAME:String = "TalkProxy";

		public function TalkProxy()
		{
			super(NAME);
		}

		override public function onRegister():void
		{
			super.onRegister();
			Connection.getInstance().registerProtocolInterest(handleTalk, [ProtocalConstants.TALK]);
		}

		override public function onRemove():void
		{
			super.onRemove();
			Connection.getInstance().unregisterHandler(handleTalk);
		}

		private function handleTalk(pack:TalkResponsePack, connection:IConnection):void
		{
			var user:User = ProxyHelper.getUser(pack.userId);
			if (!user)
			{
				return;
			}

			var talkVo:TalkVo = new TalkVo(pack.message, user);
			if (pack.roomId == -1)
			{
				sendNotification(AppConstrants.RECEIVE_LOBBY_MESSAGE, talkVo);
			}
			else
			{
				talkVo.roomId = pack.roomId;
				sendNotification(AppConstrants.RECEIVE_ROOM_MESSAGE, talkVo);
			}

		}
	}
}
